package presentation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;

import control.CControl;
import entity.ERegister;

public class PEPLNodes extends JPanel implements ComponentListener {
	private static final long serialVersionUID = 1L;
	private CControl cControl;
	private JScrollPane nodesScrollPane;
	private JPanel nodesContentPanel = new JPanel(new SpringLayout());
	private int nodeContentPanelHeight = 10;
	
	public PEPLNodes(CControl cControl) {
		this.cControl = cControl;
		this.addComponentListener(this);
		this.setBackground(Color.white);
		
		nodesScrollPane = new JScrollPane();
		nodesScrollPane.getVerticalScrollBar().setUnitIncrement(10);
		nodesScrollPane.setBackground(Color.white);
		nodesScrollPane.getVerticalScrollBar().setBackground(Color.white);
		
		nodesContentPanel.setBackground(Color.white);
		this.add(nodesScrollPane);
		
		generatePanel();
	}
	
	private void generatePanel() {
		String[] nodes = cControl.getNodeList();
		
		for (int i = 0; i < nodes.length; i++) {
			addNodePanel(nodes[i]);
		}
	}
	
	public void addNodePanel(String node){
		HashMap<String,ERegister> registers = cControl.getRegisters(node);
		Iterator<String> registerKeyset = registers.keySet().iterator();
		ERegister currentRegister;
		
		int rows = (int)Math.ceil(registers.size()/2.);
		
		nodeContentPanelHeight += 23*rows+70;
		nodesContentPanel.setPreferredSize(new Dimension(0,nodeContentPanelHeight));
		
		JLabel nodeHeader = new JLabel(node);
		nodeHeader.setFont(new Font("Arial",Font.BOLD,20));
		nodesContentPanel.add(nodeHeader);
		for (int i = 0; i < 6; i++) {
			nodesContentPanel.add(new JLabel());
		}

		int registerNumber = 0;
		Font headerFont = new Font("Arial",Font.PLAIN,16);
		while (registerKeyset.hasNext()) {
			currentRegister = registers.get(registerKeyset.next());
			String tooltip = currentRegister.getDescription();
			String unit = currentRegister.getUnit();
			if (!unit.equalsIgnoreCase("")) {
				tooltip += " [" + unit + "]";
			}
			
			String registerName = currentRegister.getName();
			
			JLabel registerNameField = new JLabel(registerName + ":");
			registerNameField.setFont(headerFont);
			registerNameField.setToolTipText(tooltip);
			registerNameField.setPreferredSize(new Dimension(200,20));
			nodesContentPanel.add(registerNameField);
			
			PRegisterValue registerValue = new PRegisterValue("-",tooltip);
			registerValue.setPreferredSize(new Dimension(600,20));
			registerValue.setHorizontalAlignment(JLabel.RIGHT);
			nodesContentPanel.add(registerValue);
			
			JLabel registerUnitField = new JLabel(unit);
			registerUnitField.setFont(headerFont);
			registerUnitField.setToolTipText(tooltip);
			registerUnitField.setMinimumSize(new Dimension(20,20));
			nodesContentPanel.add(registerUnitField);
			
			// Register field as an observer of the given value
			cControl.addRegisterObserver(node, registerName, registerValue);
			
			if ((registerNumber+1)%2 != 0) {
				JLabel spacing = new JLabel();
				spacing.setPreferredSize(new Dimension(100,20));
				nodesContentPanel.add(spacing);
			}
			
			registerNumber++;
		}
		
		for (int i = 0; i < 2*rows-registers.size(); i++) {
			nodesContentPanel.add(new JLabel());
			nodesContentPanel.add(new JLabel());
			nodesContentPanel.add(new JLabel());
			if (i < (2*rows-registers.size())-1) {
				nodesContentPanel.add(new JLabel());
			}
		}
		
		for (int i = 0; i < 7; i++) {
			JLabel nodeSpacing = new JLabel(" ");
			nodeSpacing.setFont(new Font("Arial",Font.BOLD,10));
			nodesContentPanel.add(nodeSpacing);
		}
		
		nodesScrollPane.setViewportView(nodesContentPanel);
	}

	@Override
	public void componentHidden(ComponentEvent arg0) {}

	@Override
	public void componentMoved(ComponentEvent arg0) {}

	@Override
	public void componentResized(ComponentEvent arg0) {
		SpringUtilities.makeCompactGrid(nodesContentPanel, //parent
					nodesContentPanel.getComponentCount()/7, 7,
	                3, 3,  //initX, initY
	                3, 3); //xPad, yPad
		
		nodesScrollPane.setPreferredSize(new Dimension(this.getWidth()-20,this.getHeight()-20));
		
		updateUI();
	}

	@Override
	public void componentShown(ComponentEvent arg0) {}
}
